package com.mowanka.mokeng.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.mowanka.mokeng.R
import com.mowanka.mokeng.app.base.BaseFragment
import com.mowanka.mokeng.app.ext.interceptLongClick
import com.mowanka.mokeng.databinding.FragmentMainBinding
import com.mowanka.mokeng.ui.fragment.mine.MineHomeFragment
import com.mowanka.mokeng.ui.fragment.store.StoreHomeFragment
import com.mowanka.mokeng.ui.fragment.trading.TradingHomeFragment
import com.mowanka.mokeng.ui.home.HomeFragment
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class MainFragment : BaseFragment<BaseViewModel, FragmentMainBinding>() {

    private var showStore = true

    override fun initView(savedInstanceState: Bundle?) {
        //初始化viewpager2
        mDatabind.mainViewpager.let {
            //是否可滑动
            it.isUserInputEnabled = false
            it.offscreenPageLimit = if (showStore) 5 else 4
            //设置适配器
            it.adapter = object : FragmentStateAdapter(this) {
                override fun createFragment(position: Int): Fragment {
                    return when (position) {
                        0 -> TradingHomeFragment()
                        1 -> HomeFragment()
                        2 -> StoreHomeFragment()
                        3 -> HomeFragment()
                        4 -> MineHomeFragment()
                        else -> HomeFragment()
                    }
                }

                override fun getItemCount() = if (showStore) 5 else 4
            }
        }
        //初始化 bottomBar
        mDatabind.mainBottom.let {
            it.enableAnimation(true)
            it.enableShiftingMode(false)
            it.enableItemShiftingMode(true)
            it.itemIconTintList = null
            it.setTextSize(12F)
            if (!showStore) it.menu.removeItem(R.id.navigation_notifications)

            it.setOnNavigationItemSelectedListener { item ->
                when (item.itemId) {
                    R.id.navigation_trading -> mDatabind.mainViewpager.setCurrentItem(0, false)
                    R.id.navigation_kengliao -> mDatabind.mainViewpager.setCurrentItem(1, false)
                    R.id.navigation_stroe -> mDatabind.mainViewpager.setCurrentItem(2, false)
                    R.id.navigation_message -> mDatabind.mainViewpager.setCurrentItem(3, false)
                    R.id.navigation_mine -> mDatabind.mainViewpager.setCurrentItem(4, false)
                }
                true
            }
            if (showStore)
                it.interceptLongClick(R.id.navigation_trading, R.id.navigation_kengliao, R.id.navigation_stroe, R.id.navigation_message, R.id.navigation_mine)
            else
                it.interceptLongClick(R.id.navigation_kengliao, R.id.navigation_stroe, R.id.navigation_message, R.id.navigation_mine)
        }
    }
}